package com.gm.commonlib.network.basenetwork.numberformat;

import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import com.youth.banner.util.LogUtils;

import java.io.IOException;



public class LongDefault0Adapter extends TypeAdapter<Long> {

    @Override
    public void write(JsonWriter out, Long value) throws IOException {
        out.value(value);
    }

    @Override
    public Long read(JsonReader in) throws IOException {
        if (in.peek() == JsonToken.NULL) {
            in.nextNull();
            return 0L;
        }

        String result = in.nextString();
        if(result.equals("") || result.equals("null")) {
            return 0L;
        }

        try {
            return Long.parseLong(result);
        } catch (Exception e) {
            LogUtils.d(e.toString());
            try {
                return (long)Double.parseDouble(result);
            } catch (Exception de) {
                return 0L;
            }
        }
    }
}


